package com.hzdp.web.action.review.ajax;

import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.hzdp.cache.service.CacheService;
import com.hzdp.cache.support.CacheTypeFactory;
import com.hzdp.common.support.enumeration.Platform;
import com.hzdp.review.bean.ReviewRequest;
import com.hzdp.review.bean.ReviewResponse;
import com.hzdp.review.service.ReviewService;
import com.hzdp.web.action.AjaxBaseAction;
import com.hzdp.web.captcha.JCaptchaService;
import com.hzdp.web.ops.remote.RemoteIpGetter;

public class AjaxReviewAction extends AjaxBaseAction {

	public static final String CacheType = "review-captcha-required";

	private int productId;
	private int productType;
	private String captcha;
	private int grade;
	private String info;
	private InputStream pics;
	private int age;
	private int skin;

	private boolean captchaChecked = false;

	@Autowired
	private ReviewService reviewService;
	@Autowired
	private CacheService cacheService;
	@Autowired
	private JCaptchaService jCaptchaService;

	@Override
	protected boolean checkParams() {
		int userId = getUserId();
		if (userId <= 0) {
			msg = "登录超时，请重新登录";
			type = ReviewResponse.ResponseTypeLoginRequired;
			return false;
		}
		if (StringUtils.isEmpty(info) || info.length() < 15) {
			type = ReviewResponse.ResponseTypeLengthDismatched;
			return false;
		}

		Object object = cacheService.get(CacheTypeFactory.getCacheType(CacheType), "" + userId);
		if (object != null) {
			if (StringUtils.isEmpty(captcha)) {
				msg = "评论太频繁,需要输入图形验证码。";
				type = ReviewResponse.ResponseTypeTooMuch;
				return false;
			} else {
				String captchaId = getUUID();
				if (jCaptchaService.check(captchaId, captcha)) {
					captchaChecked = true;
				} else {
					msg = "评论太频繁,需要输入图形验证码。";
					type = ReviewResponse.ResponseTypeTooMuch;
					return false;
				}
			}
		}
		return true;
	}

	@Override
	protected void executeJson() {
		// 上传图片到图片中心，并且返回urls
		ReviewRequest request = new ReviewRequest();
		request.setAge(age);
		request.setGrade(grade);
		request.setInfo(info);
		request.setIp(RemoteIpGetter.getRemoteAddr(super.request));
		request.setPlatform(Platform.Web.getValue());
		request.setProductId(productId);
		request.setProductType(productType);
		request.setSkin(skin);
		request.setCaptchaChecked(captchaChecked);
		int userId = getUserId();
		request.setUserId(userId);
		ReviewResponse reviewResponse = reviewService.addReview(request);
		int responseType = reviewResponse.getResponseType();
		if (responseType == ReviewResponse.ResponseTypeTooMuch) {
			msg = "评论太频繁,需要输入图形验证码。";
			type = ReviewResponse.ResponseTypeTooMuch;
			cacheService.put(CacheTypeFactory.getCacheType(CacheType), "" + userId, 1);
		} else if (responseType == ReviewResponse.ResponseTypeLoginRequired) {
			msg = "登录超时，请重新登录";
			type = ReviewResponse.ResponseTypeLoginRequired;
		} else if (responseType == ReviewResponse.ResponseTypeLengthDismatched) {
			msg = "内容长度不符合要求";
			type = ReviewResponse.ResponseTypeLengthDismatched;
		} else if (responseType == ReviewResponse.ResponseTypeFailed) {
			msg = "系统错误，请重试。";
			type = ReviewResponse.ResponseTypeFailed;
		} else {
			msg = "评论成功";
			type = ReviewResponse.ResponseTypeOK;
		}
	}

	@Override
	public int getType() {
		return type;
	}

	@Override
	public int getStatus() {
		return status;
	}

	@Override
	public String getMsg() {
		return msg;
	}

	public void setProductId(int productId) {
		this.productId = productId;
	}

	public void setProductType(int productType) {
		this.productType = productType;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}

	public void setInfo(String info) {
		this.info = info;
	}

//	public void setPics(InputStream pics) {
//		this.pics = pics;
//	}

}
